//
//  LJYRecEveDetailViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/25.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYRecEveDetailViewController.h"
#import "LJYRecDetailViewController.h"
#import "LJYReadTableViewCell.h"
#import "LJYReadModel.h"
#import "AppTools.h"
#import "MJRefresh.h"

@interface LJYRecEveDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *recommendlistArray;
@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, strong)UIImageView *image;
@property(nonatomic, strong)UIImageView *titleImage;
@property(nonatomic, strong)UILabel *titleLabel;


@end

@implementation LJYRecEveDetailViewController


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrame" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _recommendlistArray = [NSMutableArray array];
    self.navigationItem.title = self.ntitle;
    self.view.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self creatTableView];
}

-(void)creatTableView
{
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStyleGrouped];
    [self.view addSubview:_myTableView];
    _myTableView.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(1);
        make.top.equalTo(self.view);;
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    _myTableView.sectionHeaderHeight = 5.0;
    _myTableView.sectionFooterHeight = 11.0;
    _myTableView.rowHeight = 100.0;
    [self creatHeadView];
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    [self.myTableView.mj_header beginRefreshing];
    
}
//-(UITableViewHeaderFooterView *)
-(void)creatHeadView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 150)];
    view.backgroundColor = [UIColor blackColor];
    
    self.image = [[UIImageView alloc] initWithFrame:view.frame];
    self.image.image = [UIImage imageNamed:@"u=1280079052,3666822166&fm=21&gp=0.jpg"];
    [view addSubview:self.image];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.textColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [view addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
        make.bottom.mas_equalTo(-15);
        make.height.mas_equalTo(30);
        
    }];
    
    self.titleImage = [[UIImageView alloc] init];
    self.titleImage.backgroundColor = [UIColor blackColor];
    self.titleImage.layer.masksToBounds = YES;
    //设置半径
    self.titleImage.layer.cornerRadius = 15;
    [view addSubview:self.titleImage];
    [self.titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(view.mas_left).offset(150);
        make.right.equalTo(view.mas_right).offset(-150);
        make.bottom.equalTo(self.titleLabel.mas_top).offset(-9);
        make.height.mas_equalTo(80);
        
    }];
    
    _myTableView.tableHeaderView = view;
}

- (void)upTableView{
    
    
    [self addData: self.url andDwon:NO];
}

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    [AppTools getDataWithURL:url Block:^(id result) {
        
        if (!isDown)
        {
            [self.recommendlistArray removeAllObjects];
        }
        NSDictionary *dictionary = (NSDictionary *)result;
        NSArray *array = [dictionary objectForKey:self.ename];
        if (array.count != 0) {
            NSDictionary *title = [array objectAtIndex:0];
            self.titleLabel.text = [title objectForKey:@"alias"];
            NSURL *url = [NSURL URLWithString:[title objectForKey:@"imgsrc"]];
            [self.titleImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"u=1146796800,879196243&fm=21&gp=0.jpg"]];
        }
        
        
        for (NSDictionary *dic in array)
        {
            LJYReadModel *model = [[LJYReadModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.recommendlistArray addObject:model];
        }
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error = %@", error);
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.recommendlistArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInditify = @"cell";
    LJYReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditify];
    if (cell == nil)
    {
        cell = [[LJYReadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.read = [self.recommendlistArray objectAtIndex:indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    LJYRecDetailViewController *detail = [[LJYRecDetailViewController alloc] init];
    LJYReadModel *model = [self.recommendlistArray objectAtIndex:indexPath.section];
    detail.loadId = model.docid;
    [self.navigationController pushViewController:detail animated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
